<template>
  <el-dialog :model-value="dialogData.isShow" :title="dialogData.id !==null?'编辑':'新增'"
             @close="closeDialog" draggable>
    <el-form :model="form" ref="formRef" :rules="rules" label-width="90px">
      <el-row :gutter="10">
<!--        <el-col :span="12">-->
<!--          <el-form-item label="父ID" prop="parentId">-->
<!--            <el-input type="number" v-model="form.parentId" placeholder="请输入父ID"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :span="12">
          <el-form-item label="用户id" prop="userId">
            <el-input type="number" v-model="form.userId" placeholder="请输入用户id"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="文章id" prop="articleId">
            <el-input type="number" v-model="form.articleId" placeholder="请输入文章id"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="0：审核中" prop="isEnabled">
            <el-input v-model="form.isEnabled" maxlength="4" placeholder="请输入0：审核中"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="评论内容" prop="content">
            <el-input v-model="form.content" maxlength="255" placeholder="请输入评论内容"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {ElMessage, FormInstance, FormRules} from "element-plus";
import {addArticleComment, getArticleComment, updateArticleComment} from "@/api/app/articleComment";

const formRef = ref<FormInstance>()

const emits = defineEmits<{
  (event: 'refresh'): void
}>()

// 表单
let form: any = ref({
  id: null,
  parentId: null,
  userId: null,
  articleId: null,
  isEnabled: null,
  content: null,
});

// 效验规则
const rules = reactive<FormRules>({})

// 获取详情
const getDetails = (id: string) => {
  getArticleComment(id).then(res => {
    form.value = Object.assign({}, form.value, res);
  })
}

// 弹框数据
const dialogData = reactive({
  isShow: false,
  id: null
})

// 打开弹框
const openDialog = async (id: string) => {
  dialogData.isShow = true;
  dialogData.title = '新增文章评论';
  if (id) {
    dialogData.id = id;
    dialogData.title = '编辑文章评论';
    getDetails(id);
  }
}

// 关闭弹框
const closeDialog = () => {
  dialogData.isShow = false;
  dialogData.id = null;
  form.value = {};
}
// 提交

const submit = async () => {
  if (!formRef.value) return;
  await formRef.value.validate((valid: any) => {
    if (valid) {
      let data = form.value;
      if (data.id) {
        updateArticleComment({...data}).then(() => {
          ElMessage.success('操作成功');
          closeDialog();
          emits('refresh');
        })
      } else {
        addArticleComment({...data}).then(() => {
          ElMessage.success('操作成功');
          closeDialog();
          emits('refresh');
        })
      }
    }
  })
}

defineExpose({
  openDialog,
});
</script>

<style scoped>

</style>
